import javax.swing.tree.TreeNode;

/*
 * @lc app=leetcode.cn id=222 lang=java
 *
 * [222] 完全二叉树的节点个数
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    //前序遍历
    // int res=0;
    // public int countNodes(TreeNode root) {
    //     if(root==null) return 0;
    //     count(root);
    //     return res;
    // }
    // public void count(TreeNode root){
    //     if(root==null) return;
    //     res++;  //中
    //     count(root.left);//左
    //     count(root.right);//右
    // }
    //后序遍历
    public int countNodes(TreeNode root) {
        if(root==null) return 0;
        int left=countNodes(root.left);//左
        int right=countNodes(root.right);//右
        return left+right+1;//中
    }
}
// @lc code=end

